/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Gate with laser beam running through it               //
//-------------------------------------------------------//

//# address-hook(0x029338) end(0x029380)
function void fn029338()
{
	objA0.mapping_offset = 0x228394
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x02ea)
	objA0.box_size.x = 0x1c
	objA0.box_size.y = 0x04
	objA0.render_flags |= render_flag.WORLD
	objA0.sprite_priority = 0x0180

	objA0.countdown_value = objA0.position.y.u16 + (objA0.subtype2c & 0x0f) * 8
	objA0.value32 = ((objA0.subtype2c >> 1) & 0x78) + 8

	objA0.update_address = 0x029386
	fn029386()
}


//# address-hook(0x029386) end(0x0293ca)
function void fn029386()
{
	--u16[A0 + 0x30]
	if (s16[A0 + 0x30] < 0)
	{
		u16[A0 + 0x30] = objA0.value32

		if (allocDynamicObjectAfterA0())
		{
			fn0293d0()

			if (allocDynamicObjectAfterA0())
			{
				fn0293d0()

				objA1.sprite_priority = 0x80
				u8[A1 + 0x22] = 0x02
				objA1.collision_attributes = (collision.flag.THREAT | collision.size.4x4_2)
				objA1.update_address = 0x029416

				playSound(0xa8)
			}
		}
	}

	CheckForUnloadObject()
}


//# address-hook(0x0293d0) end(0x029414)
function void fn0293d0()
{
	objA1.update_address = 0x02941c
	objA1.position.x.u16 = objA0.position.x.u16
	objA1.position.y.u16 = objA0.position.y.u16
	u16[A1 + 0x2e] = objA0.countdown_value
	objA1.mapping_offset = 0x228394
	objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x02ea)
	objA1.box_size.x = 0x1c
	objA1.box_size.y = 0x04
	objA1.render_flags = objA0.render_flags
	u8[A1 + 0x22] = 0x01
	objA1.sprite_priority = 0x0180
}


//# address-hook(0x029416) end(0x029416)
function void fn029416()
{
	AddAsDynamicObject()
	fn02941c()
}


//# address-hook(0x02941c) end(0x029440)
function void fn02941c()
{
	if ((level.framecounter.low & 1) == 0)
	{
		objA0.render_flags ^= render_flag.FLIP_Y
	}

	D0.u16 = objA0.position.y.u16
	objA0.position.y.u16 += 4
	if (D0.u16 >= objA0.countdown_value)
	{
		objA0.position.x.u16 = 0x7ff0
	}

	DrawOrUnloadObject()
}
